package com.test.ws.s1.controller;

import com.test.ws.s1.bean.Acl_permission;
import com.test.ws.s1.bean.Acl_role_permission;
import com.test.ws.s1.service.Acl_permissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class acl_permissionController {
@Autowired
private Acl_permissionService acl_permissionService;


    /// 查询全部的权限
    @RequestMapping("/selectAllAcl_permission")
    public String selectAllAcl_permission(HttpServletRequest request){
        List<Acl_permission> aclPermissions = acl_permissionService.selectAcl_permission(null);
        System.out.println("权限管理============================"+aclPermissions);
        request.getSession().setAttribute("aclPermissions",aclPermissions);
        return "houtai/fenpeiQuanXian";
    }


    /// 权限的回显
    @RequestMapping("/selectAcl_permissionByRoleId")
    public String selectAcl_permissionByRoleId(HttpServletRequest request,Integer role_id){
        Acl_permission acl_permission = new Acl_permission();
        String roleId = request.getParameter("id");
        /// 查对应的
        List<Acl_permission> aclPermissions = acl_permissionService.selectAcl_permissionByRoleId(new Integer(roleId));
        /// 查全部
        List<Acl_permission> aclPermissions1 = acl_permissionService.selectAcl_permission(acl_permission);
        for (Acl_permission aclPermission : aclPermissions){
            for (Acl_permission aclPermission1 : aclPermissions1){
                if (aclPermission.getPerm_id() == aclPermission1.getPerm_id()){
                    aclPermission1.setIs_delete(1);
                }
            }
        }
        request.getSession().setAttribute("rid",roleId);
        request.getSession().setAttribute("aclPermissions",aclPermissions1);
        return "houtai/quanxianHx";
    }


    /// 根据id删除权限再次循环增加
    @RequestMapping("/acl_permissionDel")
    public String deleteAcl_permission(HttpServletRequest request,Integer id,Integer[] is_delete){
        String id1 = request.getParameter("rid");
        int i = acl_permissionService.deleteAcl_permission(Integer.valueOf(id1));


        if (is_delete != null){
            for (Integer isDelete : is_delete){
                Acl_role_permission aclRolePermission = new Acl_role_permission();
                aclRolePermission.setRole_id(Integer.parseInt(id1));
                aclRolePermission.setPerm_id(isDelete);
                acl_permissionService.insertAcl_permission(aclRolePermission);
            }
        }

        return "houtai/fenpeiQuanXian";
    }

}
